package com.ktwlrj;

import com.ktwlrj.securityboot.autoconfigure.annotation.EnableEncryptor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * start
 *
 * @author zhouxuanhong
 * @date 2019-09-06 18:03
 * @since JDK1.8
 */
@EnableFeignClients(basePackages = "com.ktwlrj")
@SpringBootApplication
@ComponentScan(basePackages = {"com.ktwlrj","com.ktwlsoft"})
@EnableAsync
@MapperScan("com.ktwlrj.*.mapper")
@EnableTransactionManagement
@Slf4j
@EnableEncryptor
public class Application {


    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(Application.class, args);
        printSysInfo(application);
    }


    /**
     * 打印系统关键信息
     *
     * @author zhouxuanhong
     * @date 2019-10-24 10:59
     */
    static void printSysInfo(ConfigurableApplicationContext application) throws UnknownHostException {
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path");
        log.info("\n------------------------------------------------------------------------------\n\t" +
                "Application salesAssistant is running! Access URLs:\n\t" +
                "Local: \t\t\thttp://localhost:" + port + path + "/\n\t" +
                "External: \t\thttp://" + ip + ":" + port + path + "/\n\t" +
                "swagger-ui: \t\thttp://" + ip + ":" + port + path + "/swagger-ui.html\n\t" +
                "Doc: \t\t\thttp://" + ip + ":" + port + path + "/v2/api-docs\n" +
                "------------------------------------------------------------------------------");
    }


    /**
     * 跨域处理
     *
     * @return CorsFilter ${@link CorsFilter}
     * @author zhouxuanhong
     * @date 2019-04-03 12:11
     */
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin(CorsConfiguration.ALL);
        config.addAllowedHeader(CorsConfiguration.ALL);
        config.setMaxAge(18000L);
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("POST");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }


    @Bean
    public ModelMapper modelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setFullTypeMatchingRequired(true);
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
        modelMapper.getConfiguration().setSkipNullEnabled(true);
        return modelMapper;
    }


    /**
     * 解决RequestContextHolder.getRequestAttributes()空指针问题
     *
     * @return RequestContextListener ${@link RequestContextListener}
     * @author zhouxuanhong
     * @date 2019-06-03 12:23
     */
    @Bean
    public RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }

}
